import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterapp2/bugly/report.dart';
import 'package:flutterapp2/building/buildinfo.dart';
import 'package:flutterapp2/page/TipRoute.dart';
import 'package:flutterapp2/page/testrequest.dart';
import 'package:flutterapp2/test/test.dart';
import 'package:flutterapp2/test/test_anim.dart';
import 'package:flutterapp2/test/test_container.dart';
import 'package:flutterapp2/test/test_form.dart';
import 'package:flutterapp2/test/test_notification.dart';
import 'package:flutterapp2/util/navigator_util.dart';
import 'package:provider/provider.dart';

import 'business/special_house_widget.dart';
import 'page/NewRoute.dart';
import 'page/buildingmainpage.dart';
import 'page/idatewidget.dart';
import 'sliver/sliver2.dart';
import 'sliver/sliver2.dart';
import 'sliver/test_sliver.dart';
import 'test/test_anim2.dart';
import 'test/test_lifecycler.dart';
import 'test/test_parse.dart';
import 'test/test_provider.dart';
import 'test/test_sp.dart';
import 'test/test_sticky.dart';
import 'test/testcard.dart';

void main() {
  launch(MyApp());
//    runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      initialRoute: "/",
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
//        "newpage": (context) => NewRoute(),
        "tip2": (context) {
          // 所传的参数为路由传入的参数
          return TipRoute(text: ModalRoute.of(context).settings.arguments);
        },
        "/": (context) => MyHomePage(title: 'Flutter Demo Home Page'),
        //注册首页路由
      },
      onGenerateRoute: (settings) {
        return MaterialPageRoute(builder: (context) {
          String routeName = settings.name;
          if (routeName == "newpage") {
            // 判断是否登录，
            return NewRoute();
          }
          return null;
          // 如果访问的路由页需要登录，但当前未登录，则直接返回登录页路由，
          // 引导用户登录；其它情况则正常打开路由
        });
      },
//      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

/// 当按钮点击时，会调用此函数，该函数的作用是先自增_counter，然后调用setState 方法。
/// setState方法的作用是通知Flutter框架，有状态发生了改变，
/// Flutter框架收到通知后，会执行build方法来根据新的状态重新构建界面，
/// Flutter 对此方法做了优化，使重新执行变的很快，所以你可以重新构建任何需要更新的东西，而无需分别去修改各个widget。
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new ListView(
          children: <Widget>[
            _createRoute("test anim2", SVPersonalInfoPage()),
            _createRoute("test anim", ExpertHomePage()),
            _createRoute("test notification", TestNotification()),
            _createRoute("test provider", TestProvider()),
            _createRoute("test card", CardWidget()),
            _createRoute("test form", TestFormPage()),
            _createRoute("test container", TestContainer()),
            _createRoute("test request", TestRequestPage()),
            _createRoute("test sticky and anchor", TestStickyPage()),
            _createRoute("test sharedpreferences", TestSharedPreferences()),
            _createRoute("test lifecycler", TestLifecyclePage()),
            _createRoute("test parse", TestParsePage()),
            _createRoute("test1", Test1Page()),
            _createRoute(
                "business",
                ListView(
                  children: <Widget>[SpecialHouseWidget()],
                )),
            FlatButton(
              child: Text("open new route"),
              textColor: Colors.blue,
              onPressed: () async {
                // 跳转传值，并等待回传
                List<BuildInfo> list = new List();
                list.add(BuildInfo(id: "10003"));
//                Navigator.pushNamed(context, "newpage",arguments: "111");
                var result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return BuildingMainPage(
                    selected: list,
                  );
                }));
                print(result.toString());
              },
            ),
            FlatButton(
                child: Text("test InheritedWidget"),
                textColor: Colors.blue,
                onPressed: () {
                  /// 传递多条数据看情况：如果是类型不一致的数据，可以使用MultiProvider，如果存在类型一致的数据可以用Map传递
                  Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                    return ChangeNotifierProvider.value(
                      value: BuildInfo(id: "100", name: "张三"),
                      child: InheritedWidgetTestRoute(),
                    );

                    /// fixme 注意因为运行时问题，编译器会根据值推到泛型类型，传入和查找的数据必须对应，简单的方法是，as Map，of的时候也使用Map
//                    return Provider.value(
//                        value: {
//                          "id": 1001,
//                          "name": "张三",
//                        } as Map,
//                        child: InheritedWidgetTestRoute());

//                    return MultiProvider(
//                      providers: [
//                        Provider.value(value: BuildInfo(id: "100", name: "张三")),
//                        Provider.value(value: "111"),
//                        Provider.value(value: "222"), // 如果有两个String呢
//                        Provider.value(value: [222]),
//                      ],
//                      child: InheritedWidgetTestRoute(),
//                    );

//                    return DataProvider(
//                      data: BuildInfo(id: "100", name: "张三"),
//                      child: InheritedWidgetTestRoute(),
//                    );
                  }));
                }),
          ],
        ),
      ),
    );
  }

  Widget _createRoute(String title, Widget scene) {
    return FlatButton(
      onPressed: () {
        NavigatorUtil.push(context, scene);
      },
      child: Text(title),
      textColor: Colors.blue,
    );
  }
}
